<?php


namespace app\common\library;

use app\common\library\dingding\AliDingDing;
use app\api\model\IdcModel;

class IdcChecker
{
    function CheckIsIDCard($id_card){
        if(mb_strlen($id_card) != 18)
            return false;
        //校验位列表
        $remainder_list = [1,0,'X',9,8,7,6,5,4,3,2];
        //加权除以11的余数
        $square_remainder = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
        //取得身份证号码最后一位校验位
        $check_num = mb_substr($id_card,17);
        //身份证参与校验的前17位
        $id_card = mb_substr($id_card,0,17);
        //参与校验的必须是数字
        if(!is_numeric($id_card))
            return false;
        $square_sum = 0;//每一位参与校验数乘以加权数的累加和
        $number_sum = 0;//每一位参与校验数乘以加权除以11的余数的累加和
        for ($i = 0;$i < 17;$i++){
            //累加结果
            $square_sum += intval($id_card[$i]) * pow(2,17-$i);
            //累加结果
            $number_sum += intval($id_card[$i]) * $square_remainder[$i];
        }
        //从校验位列表中获取加权乘积和得到的校验位
        $check_get_square_remainder = isset($remainder_list[$square_sum%11])?$remainder_list[$square_sum%11]:-1;
        //从校验位列表中获取加权余数乘积和得到的校验位
        $check_get_number_remainder = isset($remainder_list[$number_sum%11])?$remainder_list[$number_sum%11]:-1;
        if($check_get_square_remainder == $check_num && $check_get_number_remainder == $check_num)
            return true;

        return false;
    }

    function check_fuqing($name, $idcno)
    {
        $ret = (new IdcModel())->is_cached($idcno, $name);
        if ($ret >= 0) {
            return $ret; //0=ok,1=no
        }

        $url = 'https://idcert.market.alicloudapi.com/idcard';
        $url = 'https://naidcard.market.alicloudapi.com/nidCard';
        $params = [
            'idCard' => $idcno,
            'name' => $name,
        ];
        $headers = [
            "Authorization:APPCODE " . 'e756286ae9ef480cbbeb89b1dd6a514c',
            //   "Authorization:APPCODE " . '23350e83e2224a16a1a3e440a1fbd168',
        ];
        $result = \common::curlget($url, $params, $headers);
        $j = null;
        if ($result) {
            $j = json_decode($result, true);
        }
        if (empty($j)) {
            $ding = new AliDingDing(config('app.system_warning_ding_ding_token'));
            $logStr = "四川涪擎身份认证异常";
            $ding->assign('text', $logStr)
                ->at(true)
                ->send($logStr);
            return -1;
        }

        $status = -1;
        if ($j['status'] == '01') {
            $status = 0;
        }
        $extra = [
            'auditor' => 'fuqing',
        ];
        (new IdcModel())->add($idcno, $name, $status, $extra);
        return $status;
    }

    function check_ocr($name,$idcno){
        $ret = (new IdcModel())->is_cached($idcno, $name);
        if ($ret >= 0) {
            return $ret; //0=ok,1=no
        }

        $url = 'https://lfeid.market.alicloudapi.com/idcheck/life';
        $params = [
            'cardNo' => $idcno,
            'realName' => $name,
        ];
        $headers = [
            "Authorization:APPCODE " . '52b52bf680f54a479762a3a7a1c22192',
            //   "Authorization:APPCODE " . '23350e83e2224a16a1a3e440a1fbd168',
        ];
        $result = \common::curlget($url, $params, $headers);
        $j = null;
        if ($result) {
            $j = json_decode($result, true);
        }
        if (empty($j)) {
            $ding = new AliDingDing(config('app.system_warning_ding_ding_token'));
            $logStr = "API认证异常";
            $ding->assign('text', $logStr)
                ->at(true)
                ->send($logStr);
            return -1;
        }

        $status = -1;
        if ($j['error_code'] == 0) {
            if ($j['result']['isok'] == true){
                $status = 0;
            }

        }
        $extra = [
            'auditor' => 'hulian',
        ];
        (new IdcModel())->add($idcno, $name, $status, $extra);
        return $status;
    }
}